<?php
class News extends Public_Controller{
	
	//public $namespace = 'categories';
	
	function __construct(){
		parent::__construct();
		$this->load->model('article_m');
		$this->template
			//->set_layout('body.html')
			->append_css('block.css')
			->append_css('news.css');
		menu_active();
		//$this->output->cache(10000000);
	}
	
	function index($cat_id = NULL , $page_index = 0){
		
		$base_where = array();
		$base_where = $page_index ? $base_where + array('limit' => $page_index) : $base_where;
		$base_where = $cat_id ? $base_where + array('cat_id' => $cat_id) : $base_where;
		
		$model = $this->article_m->get_many_by($base_where,$this->config_module->page_size);
		$this->load->helper('kunkun_page');
		$total_page = total_page($model->total_row,$this->config_module->page_size);
		//print_r($model->articles);exit;
		$this->template
			//->set_breadcrumb(lang('news:news'))
			->title(lang('news:news'))
			->set('model',$model)
			->set('keywords',$this->config_module->keywords)
			->set('description',$this->config_module->description)
			->set('articles',$model->articles)
			->build($this->controller.'/'.$this->method);
	}
	
	function categories( $slug , $page_index = 0 ){
		
		$page_index = ($page_index > 0) ? ($page_index - 1) : 0;
		$model = $this->article_m->categories( $slug,$this->config_module->page_size,$page_index );//categories articles total_row
		
		$this->load->helper('kunkun_page');
		//$total_page = total_page($model->total_row,$this->config_module->page_size);
		
		
		$this->load->library('pagination');
		 $config['base_url'] = base_url('news/categories/'.$slug);
		 $config['total_rows'] = $model->total_row;//$model->total_page;
		 $config['per_page']	= $this->config_module->page_size;
		 $config['uri_segment'] = $this->uri->segment(4) ? 4 : 0;
		 $config['use_page_numbers'] = TRUE;
		 $config['next_tag_open'] = '<span>';
		 $config['next_tag_close'] = '</span>';
		 $config['prev_tag_open'] = '<span>';
		 $config['prev_tag_close'] = '</span>';
		 
		 $this->pagination->initialize($config); 
		 $pagination = $this->pagination->create_links();
		 
		$this->template
			->set_breadcrumb(lang('news:news'),'news')
			->set_breadcrumb($model->categories->title)
			->set('articles',$model->articles)
			//->set('total_page',$total_page)
			->set('slug',$slug)
			->set('pagination',$pagination)
			->title(lang('news:news'))
		->build('index');
	}
	
	function detail($slug){
		$model = $this->article_m->detail( $slug );
		
		$comment = $this->config_module->comment;
		$comment_data = new stdClass();
		if($comment == 1){
			$this->load->model('comment/comment_m');
			$comment_data = $this->comment_m->get_all($this->module,$model->id);
		}
		$this->template
			->set(array(
				'model'=>$model,
				'config' => $this->config_module,
				'comment_data' => $comment_data
			))
			->append_css('comment.css')
			->append_css('ui/jquery-ui.min.css')
			->append_css('dialog.css')
			->append_js('ui/jquery-ui.min.js')
			->build('detail');
		
	}
}